/**
 * 
 */
package cn.crm.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import cn.crm.bean.AddressBook;
import cn.crm.dao.AddressBookDao;

/**
 * @author wbh
 * 
 */
@Repository("addressBookDao")
public class AddressBookDaoImpl extends HibernateDaoSupport implements
		AddressBookDao {
	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.crm.dao.AddressBookDao#save(cn.crm.bean.AddressBook)
	 */
	@Override
	public String save(AddressBook addressBook) {
		// TODO Auto-generated method stub
		return (String) super.getHibernateTemplate().save(addressBook);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.crm.dao.AddressBookDao#upd(cn.crm.bean.AddressBook)
	 */
	@Override
	public Integer upd(AddressBook addressBook) {
		// TODO Auto-generated method stub
		Integer result = 0;
		super.getHibernateTemplate().update(addressBook);
		result = 1;
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.crm.dao.AddressBookDao#del(cn.crm.bean.AddressBook)
	 */
	@Override
	public Integer del(AddressBook addressBook) {
		Integer result = 0;
		super.getHibernateTemplate().update(addressBook);
		result = 1;
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.crm.dao.AddressBookDao#show()
	 */
	@Override
	public List<AddressBook> show(Integer pagesize, Integer currpage) {
		// TODO Auto-generated method stub
		Session session = super.getSession();
		Query query = session
				.createQuery("from AddressBook where abIsdelete = false");
		query.setFirstResult((currpage - 1) * pagesize);
		query.setMaxResults(pagesize);
		return query.list();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.crm.dao.AddressBookDao#get(java.lang.Integer)
	 */
	@Override
	public AddressBook get(String id) {
		// TODO Auto-generated method stub
		return super.getHibernateTemplate().get(AddressBook.class, id);
	}

	@Override
	public List<AddressBook> showByName(AddressBook book) {

		String hql = "from AddressBook where customer.custName  like '%"
				+ (book.getCustomer().getCustName() == null ? "" : book
						.getCustomer().getCustName())
				+ "%' and DDLByTypeDepartments.typeValue like '%"
				+ (book.getDDLByTypeDepartments().getTypeValue() == null ? ""
						: book.getDDLByTypeDepartments().getTypeValue())
				+ "%' and DDLByTypeSex.typeValue like '%"
				+ (book.getDDLByTypeSex().getTypeValue() == null ? "" : book
						.getDDLByTypeSex().getTypeValue())
				+ "%' and DDLByTypeDutiesname.typeValue like '%"
				+ (book.getDDLByTypeDutiesname().getTypeValue() == null ? ""
						: book.getDDLByTypeDutiesname().getTypeValue())
				+ "%' and abTellphone like '%"
				+ (book.getAbTellphone() == null ? "" : book.getAbTellphone())
				+ "%' and abProname like '%"
				+ (book.getAbProname() == null ? "" : book.getAbProname())
				+ "%' and abInterest like '%"
				+ (book.getAbInterest() == null ? "" : book.getAbInterest())
				+ "%' and abAddress like '%"
				+ (book.getAbAddress() == null ? "" : book.getAbAddress())
				+ "%' and abIsdelete = false";

		return super.getHibernateTemplate().find(hql);
	}

	@Override
	public List<AddressBook> showChange(AddressBook book, Integer pagesize,
			Integer currpage) {
		Session session = super.getSession();
		String hql = "";
		if (book.equals("")) {
			hql = "from AddressBook where customer.custName  like '%"
					+ (book.getCustomer().getCustName() == null ? "" : book
							.getCustomer().getCustName())
					+ "%' and DDLByTypeDepartments.typeValue like '%"
					+ (book.getDDLByTypeDepartments().getTypeValue() == null ? ""
							: book.getDDLByTypeDepartments().getTypeValue())
					+ "%' and DDLByTypeSex.typeValue like '%"
					+ (book.getDDLByTypeSex().getTypeValue() == null ? ""
							: book.getDDLByTypeSex().getTypeValue())
					+ "%' and DDLByTypeDutiesname.typeValue like '%"
					+ (book.getDDLByTypeDutiesname().getTypeValue() == null ? ""
							: book.getDDLByTypeDutiesname().getTypeValue())
					+ "%' and abTellphone like '%"
					+ (book.getAbTellphone() == null ? "" : book
							.getAbTellphone())
					+ "%' and abProname like '%"
					+ (book.getAbProname() == null ? "" : book.getAbProname())
					+ "%' and abInterest like '%"
					+ (book.getAbInterest() == null ? "" : book.getAbInterest())
					+ "%' and abAddress like '%"
					+ (book.getAbAddress() == null ? "" : book.getAbAddress())
					+ "%' and abIsdelete = false";
		} else {
			hql = "from AddressBook where abIsdelete = false";
		}
		Query query = session.createQuery(hql);
		query.setFirstResult((currpage - 1) * pagesize);
		query.setMaxResults(pagesize);
		return query.list();
	}

	@Override
	public Integer count() {
		// TODO Auto-generated method stub
		return super.getHibernateTemplate()
				.find("from AddressBook where abIsdelete = false").size();
	}

}
